package com.sbtr.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.system.dto.DictionaryDto;
import com.sbtr.business.system.dto.DictionaryTreeDto;
import com.sbtr.business.system.entity.Dictionary;
import com.sbtr.business.system.entity.DictionaryParam;
import com.sbtr.business.system.service.DictionaryParamService;
import com.sbtr.business.system.service.DictionaryService;
import com.sbtr.business.utils.AnalyzeUtil;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.controller.common.CommonController;
import com.sbtr.exception.BDException;
import com.sbtr.util.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据字典字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-03-22
 */
@RestController
@RequestMapping("/dictionary")
@Api(tags = "数据字典字段列表")
public class DictionaryController extends CommonController<Dictionary, DictionaryService> {

    @Autowired
    AnalyzeUtil analyzeUtil;

    @GetMapping("/List")
    @ApiOperation(httpMethod = "GET", value = "列表")
    public Response<List<Dictionary>> list(Dictionary dictionary) {
        QueryWrapper<Dictionary> wrapper = new QueryWrapper<>(dictionary);
        wrapper.orderByAsc("number");
        return Response.success(dictionary.selectList(wrapper));
    }

    @GetMapping("/TreeList")
    @ApiOperation(httpMethod = "GET", value = "字典树结构目录，只有二级")
    public Response<List<DictionaryTreeDto>> treeList(String dirCodes,Dictionary dictionary) {
        return Response.success(u.treeList(dirCodes,dictionary));
    }


    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "数据字典字段列表分页列表")
    public Response<PageDto<Dictionary>> pageList(DictionaryDto dictionaryDto) {
        return Response.success(u.pageList(dictionaryDto));
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "修改")
    public Response<String> update(@RequestBody Dictionary dictionary) {
        List<Dictionary> list = dictionary.selectList(new QueryWrapper<>());

        if (StringUtils.isNotBlank(dictionary.getDirCode())) {
            for (Dictionary dictionary1:list) {
                if(dictionary.getDirCode().equals(dictionary1.getDirCode())){
                    throw new BDException("编号已经存在");
                }
            }
        }
//        if (StringUtils.isNotBlank(dictionary.getDirName())) {
//            for (Dictionary dictionary1:list) {
//                if(dictionary.getDirName().equals(dictionary1.getDirName())){
//                    throw new BDException("名称已经存在");
//                }
//            }
//        }

        Dictionary dictionary1 = dictionary.selectById();
        if(dictionary1.getIsSystem()==0){
            throw new BDException("系统参数不能修改");
        }

        if (!dictionary.updateById()) {
            throw new BDException("修改失败");
        }
        analyzeUtil.flushDictionary();

        return Response.success("修改成功");
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "数据字典字段列表保存")
    public Response<Dictionary> save(@RequestBody Dictionary dictionary) {

        List<Dictionary> list = dictionary.selectList(new QueryWrapper<>());
//        if (dictionary.getNumber() != null) {
//            for (Dictionary dictionary1:list) {
//                if(dictionary.getNumber().equals(dictionary1.getNumber())){
//                    throw new BDException("序号已经存在");
//                }
//            }
//        }

        if (StringUtils.isNotBlank(dictionary.getDirCode())) {
            for (Dictionary dictionary1:list) {
                if(dictionary.getDirCode().equals(dictionary1.getDirCode())){
                    throw new BDException("编号已经存在");
                }
            }
        }
//        if (StringUtils.isNotBlank(dictionary.getDirName())) {
//            for (Dictionary dictionary1:list) {
//                if(dictionary.getDirName().equals(dictionary1.getDirName())){
//                    throw new BDException("名称已经存在");
//                }
//            }
//        }

        dictionary.setId(null);
        if (dictionary.insert()) {
            analyzeUtil.flushDictionary();

            return Response.success(dictionary);
        } else throw new BDException("保存失败");
    }

//    @PostConstruct
//    public void initDicOPT(){
//        //根据某个类型初始化处理字典
//        String key="filter_";
//        String keyName="过滤处理";
//
//        Map<String,String> opt= new HashMap<>();
//        opt.put("missing","缺失值处理");
//        opt.put("duplicate","重复值处理");
//        opt.put("attributecreate","属性生成处理");
//        opt.put("standard","数据标准化");
//        opt.put("pca","主成分分析");
//
//        QueryWrapper<Dictionary> dictionaryWrapper = new QueryWrapper<>();
//        dictionaryWrapper.like("dir_code",key);
//        List<Dictionary> list = u.list(dictionaryWrapper);
//
//
//
//    }


    @PostMapping("/deleteByIds")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据id批量删除,逗号分隔ps:'1,2,3'，关联子表删除请重写")
    public Response<String> deleteByIds(String ids){
        List<String> list = Arrays.asList(ids.split(","));

        List<Dictionary> dictionaryList = u.list(new QueryWrapper<Dictionary>().in("id", list));

        for (Dictionary dictionary1:dictionaryList) {
            if(dictionary1.getIsSystem()==0){
                throw new BDException("系统级不可删除");
            }
        }

        if(!u.removeByIds(list)){
            throw new BDException("删除失败");
        }
        analyzeUtil.flushDictionary();

        return Response.success("删除成功");
    }
}

